
import { reqAddressList, reqDelAddress } from '../../../../../pages/api/address'
const app = getApp()
Page({
  // 页面的初始数据
  data: {
    addressList: []
  },

  onLoad(options) {
    this.flag = options.flag

  },
  changeAddress(event) {
    const id = event.currentTarget.dataset.id
    if (this.flag != 1) return
    const newList = this.data.addressList.find((item) => item.id === id)
    if (newList) {
      app.globalData.address = newList
      wx.navigateBack()
    }


  },
  async getAddress() {
    const res = await reqAddressList()
    this.setData({
      addressList: res.data
    })
  },

















  async onShow() {
    this.getAddress()
  },
  // 去编辑页面
  toEdit(event) {

    const { id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/modules/settingModule/pages/address/add/index?id=${id}`
    })
  },
  /**
   * 删除指定收货地址
   * @param {id} event 
   */
  async delAddress(event) {
    const { id } = event.currentTarget.dataset
    const del = await wx.showModal({
      title: '您确定要删除地址吗？',
    })
    if (del) {
      await reqDelAddress(id)
      wx.showToast({
        title: '删除成功',
        duration: 2000
      })
      this.getAddress()
    }
  },
})
